﻿package com.thorqq.magictimer.timeLoop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.thorqq.magictimer.AdapterChooseMonthWeekDay;
import com.thorqq.magictimer.R;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TLoopPolicy;
import com.thorqq.magictimer.util.Util;
import com.thorqq.magictimer.widget.GridViewWithDivider;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

public class ChildViewLoopMonWeek extends ChildView
{
    @SuppressWarnings("unused")
    private ChildViewParent mParent;
    private TLoopPolicyMon mPolicy;
    private Context mContext;
    private LayoutInflater mInflater;

    private ArrayList<Map<String, Object>> mMonthList = new ArrayList<Map<String, Object>>();
    private AdapterChooseMonthWeekDay mMonthAdapter;
    private GridViewWithDivider mMonthGridView;

    private ArrayList<Map<String, Object>> mMonthWeekList = new ArrayList<Map<String, Object>>();
    private AdapterChooseMonthWeekDay mMonthWeekAdapter;
    private GridViewWithDivider mMonthWeekGridView;

    private ArrayList<Map<String, Object>> mWeekdayList = new ArrayList<Map<String, Object>>();
    private AdapterChooseMonthWeekDay mWeekdayAdapter;
    private GridViewWithDivider mWeekdayGridView;

    public ChildViewLoopMonWeek(Context context, TLoopPolicy policy)
    {
        mContext = context;
        mPolicy = (TLoopPolicyMon) policy;
    }

    @Override
    public void updateLayout()
    {

    }

    @Override
    protected void registerListener()
    {

    }

    @Override
    protected void doInitLayout(ChildViewParent parent)
    {
        Util.log("init ChildViewLoopMonWeek");
        mInflater = LayoutInflater.from(mContext);
        mView = mInflater.inflate(R.layout.loop_item_child_mon_week, null);
        mParent = parent;

        // 月
        mMonthGridView = ((GridViewWithDivider) mView.findViewById(R.id.GridViewWithDividerMonth));
        mMonthAdapter = new AdapterChooseMonthWeekDay(mContext, mMonthList);
        mMonthGridView.setAdapter(mMonthAdapter);
        mMonthGridView.setHorizontalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_hor));
        mMonthGridView.setHorizontalDividerHeight(1);
        mMonthGridView.setVerticalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_ver));
        mMonthGridView.setVerticalDividerWidth(1);
        mMonthGridView.setColumnNum(6);
        
        mMonthList.clear();
        String[] monthText = {"1","2","3","4","5","6","7","8","9","10","11","12"};
        for(int i = 0; i < monthText.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text", monthText[i]);
            map.put("mask", mPolicy.getMonthMask());
            map.put("listener", new MonthGridListener(mContext, i));
            mMonthList.add(map);            
        }
        mMonthAdapter.notifyDataSetChanged();   
        
        // 月内周
        mMonthWeekGridView = ((GridViewWithDivider) mView.findViewById(R.id.GridViewWithDividerMonthWeek));
        mMonthWeekAdapter = new AdapterChooseMonthWeekDay(mContext, mMonthWeekList);
        mMonthWeekGridView.setAdapter(mMonthWeekAdapter);
        mMonthWeekGridView.setHorizontalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_hor));
        mMonthWeekGridView.setHorizontalDividerHeight(1);
        mMonthWeekGridView.setVerticalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_ver));
        mMonthWeekGridView.setVerticalDividerWidth(1);
        mMonthWeekGridView.setColumnNum(5);
        
        mMonthWeekList.clear();
        String[] monthWeekText = {"1","2","3","4","-1"};
        for(int i = 0; i < monthWeekText.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text", monthWeekText[i]);
            map.put("mask", mPolicy.getWeekMask());
            map.put("listener", new MonthWeekGridListener(mContext, i));
            mMonthWeekList.add(map);            
        }
        mMonthWeekAdapter.notifyDataSetChanged();   
        
        
        //周内日
        mWeekdayGridView = ((GridViewWithDivider) mView.findViewById(R.id.GridViewWithDividerWeekday));
        mWeekdayAdapter = new AdapterChooseMonthWeekDay(mContext, mWeekdayList);
        mWeekdayGridView.setAdapter(mWeekdayAdapter);
        mWeekdayGridView.setHorizontalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_hor));
        mWeekdayGridView.setHorizontalDividerHeight(1);
        mWeekdayGridView.setVerticalDivider(mView.getResources().getDrawable(R.drawable.gridview_divider_ver));
        mWeekdayGridView.setVerticalDividerWidth(1);
        mWeekdayGridView.setColumnNum(7);
        
        mWeekdayList.clear();
        String[] weekdayText = {"日","一","二","三","四","五","六"};
        for(int i = 0; i < weekdayText.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("text", weekdayText[i]);
            map.put("mask", mPolicy.getWeekDayMask());
            map.put("listener", new WeekdayGridListener(mContext, i));
            mWeekdayList.add(map);            
        }
        mWeekdayAdapter.notifyDataSetChanged();   

    }

    @Override
    protected void doSetTag(Object object)
    {
        
    }

    class MonthGridListener implements OnClickListener 
    {
        private int mPos;
        @SuppressWarnings("unused")
        private Context mContext;

        MonthGridListener(Context context, int pos) 
        {
            mContext = context;
            mPos = pos;
        }
        
        @Override
        public void onClick(View v) 
        {
            char[] monthMask = mPolicy.getMonthMask();
            if(mPos > monthMask.length)
            {
                return;
            }

            if(monthMask[mPos] == '1')
            {
                monthMask[mPos] = '0';
            }
            else
            {
                monthMask[mPos] = '1';
            }
            mPolicy.setMonthMask(monthMask);
            
            mMonthAdapter.notifyDataSetChanged();   
        }
    }
    
    class MonthWeekGridListener implements OnClickListener 
    {
        private int mPos;
        @SuppressWarnings("unused")
        private Context mContext;

        MonthWeekGridListener(Context context, int pos) 
        {
            mContext = context;
            mPos = pos;
        }
        
        @Override
        public void onClick(View v) 
        {
            char[] weekMask = mPolicy.getWeekMask();
            if(mPos > weekMask.length)
            {
                return;
            }

            if(weekMask[mPos] == '1')
            {
                weekMask[mPos] = '0';
            }
            else
            {
                weekMask[mPos] = '1';
            }
            mPolicy.setWeekMask(weekMask);
            
            mMonthWeekAdapter.notifyDataSetChanged();   
        }
    }

    class WeekdayGridListener implements OnClickListener 
    {
        private int mPos;
        @SuppressWarnings("unused")
        private Context mContext;

        WeekdayGridListener(Context context, int pos) 
        {
            mContext = context;
            mPos = pos;
        }
        
        @Override
        public void onClick(View v) 
        {
            char[] weekdayMask = mPolicy.getWeekDayMask();
            if(mPos > weekdayMask.length)
            {
                return;
            }

            if(weekdayMask[mPos] == '1')
            {
                weekdayMask[mPos] = '0';
            }
            else
            {
                weekdayMask[mPos] = '1';
            }
            mPolicy.setWeekDayMask(weekdayMask);
            
            mWeekdayAdapter.notifyDataSetChanged();   
        }
    }

    @Override
    public void onClose()
    {
        
    }

}
